package com.huashi.dealer.modular.controller.api.verify;

import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.verify.service.VerifyService;
import com.huashi.dealer.modular.business.verify.param.VerifyRequestParam;
import com.huashi.dealer.modular.business.verify.param.VerifyResponseParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * API验证码控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API验证码控制器")
@RestController("apiVerifyController")
@RequestMapping("/api/verify")
@Validated
public class VerifyController {

    @Resource
    private VerifyService verifyService;

    /**
     * 获取验证码
     */
    @GetMapping("/code")
    @Operation(summary = "获取验证码")
    public CommonResult<VerifyResponseParam> getVerifyCode(@RequestParam(required = false) String token,
                                                           @RequestParam(required = false) String type) {
        try {
            VerifyResponseParam responseParam = verifyService.getVerifyCode(token, type);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取验证码失败：" + e.getMessage());
        }
    }

    /**
     * 验证验证码
     */
    @PostMapping("/check")
    @Operation(summary = "验证验证码")
    public CommonResult<String> checkVerifyCode(@RequestBody VerifyRequestParam requestParam) {
        try {
            verifyService.checkVerifyCode(requestParam);
            return CommonResult.ok("验证成功");
        } catch (Exception e) {
            return CommonResult.error("验证验证码失败：" + e.getMessage());
        }
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/sms/send")
    @Operation(summary = "发送短信验证码")
    public CommonResult<String> sendSmsCode(@RequestBody VerifyRequestParam requestParam) {
        try {
            verifyService.sendSmsCode(requestParam);
            return CommonResult.ok("发送成功");
        } catch (Exception e) {
            return CommonResult.error("发送短信验证码失败：" + e.getMessage());
        }
    }

    /**
     * 验证短信验证码
     */
    @PostMapping("/sms/check")
    @Operation(summary = "验证短信验证码")
    public CommonResult<String> checkSmsCode(@RequestBody VerifyRequestParam requestParam) {
        try {
            verifyService.checkSmsCode(requestParam);
            return CommonResult.ok("验证成功");
        } catch (Exception e) {
            return CommonResult.error("验证短信验证码失败：" + e.getMessage());
        }
    }

    /**
     * 发送邮箱验证码
     */
    @PostMapping("/email/send")
    @Operation(summary = "发送邮箱验证码")
    public CommonResult<String> sendEmailCode(@RequestBody VerifyRequestParam requestParam) {
        try {
            verifyService.sendEmailCode(requestParam);
            return CommonResult.ok("发送成功");
        } catch (Exception e) {
            return CommonResult.error("发送邮箱验证码失败：" + e.getMessage());
        }
    }

    /**
     * 验证邮箱验证码
     */
    @PostMapping("/email/check")
    @Operation(summary = "验证邮箱验证码")
    public CommonResult<String> checkEmailCode(@RequestBody VerifyRequestParam requestParam) {
        try {
            verifyService.checkEmailCode(requestParam);
            return CommonResult.ok("验证成功");
        } catch (Exception e) {
            return CommonResult.error("验证邮箱验证码失败：" + e.getMessage());
        }
    }

    /**
     * 获取图形验证码
     */
    @GetMapping("/image")
    @Operation(summary = "获取图形验证码")
    public CommonResult<VerifyResponseParam> getImageCode(@RequestParam(required = false) String token,
                                                          @RequestParam(required = false) String type) {
        try {
            VerifyResponseParam responseParam = verifyService.getImageCode(token, type);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取图形验证码失败：" + e.getMessage());
        }
    }

    /**
     * 验证图形验证码
     */
    @PostMapping("/image/check")
    @Operation(summary = "验证图形验证码")
    public CommonResult<String> checkImageCode(@RequestBody VerifyRequestParam requestParam) {
        try {
            verifyService.checkImageCode(requestParam);
            return CommonResult.ok("验证成功");
        } catch (Exception e) {
            return CommonResult.error("验证图形验证码失败：" + e.getMessage());
        }
    }
} 